//
//  XYReadBookIndexController.swift
//  XYReadBook
//
//  Created by fox on 2017/9/29.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit
import MJRefresh
/// 阅读控制器 里的 目录控制器
class XYReadBookIndexController: XYViewController {

    var didSelectRowAt:((_ row:Int)->Void)?
    let Identifier = "BookListIdentifier"
    let bookID:String
    var dataArr:[Chapters] = [] {
        didSet {
            tableView.reloadData()
        }
    }
    private lazy var tableView:UITableView = {
        let tableView = UITableView.init(frame: CGRect.zero, style: .plain)
        tableView.backgroundColor = UIColor.hm_color(withRed: 242, green: 242, blue: 242)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.register(XYBookListCell.self, forCellReuseIdentifier:self.Identifier)
        tableView.mj_header = MJRefreshHeader.init(refreshingBlock: {
            XYLog(messsage: "下拉刷新")
            tableView.mj_header.endRefreshing()
        })
        return tableView
    }()
    
    init(bookID:String) {
        self.bookID = bookID
        super.init(nibName: nil, bundle: nil)
    }

    deinit {
        XYLog(messsage: "我是目录死了")
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }


    func setupUI() {

        view.addSubview(tableView)
    }


    func close() {
        navigationController?.popViewController(animated: true)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tableView.frame = CGRect.init(x: 0, y: 0, width: view.width, height: view.height)
    }

    func Refresh() {
        let dict = ["bookId":bookID] as [String : Any]
        let url = BaseUrl.defult.rawValue + "mix-atoc/" + bookID
        HTTPManager.requestModel(url: url, method: .get, parameters: dict, iscaChe: true) { (resp:XYChapterListModel?, error) in
            if let resp = resp {
                if let chapters = resp.mixToc?.chapters {
                    self.dataArr = chapters
                    XYLog(messsage: "刷新数据了")
                }
            }else {
                // 暂无更多章节
            }
        }

    }
}


extension XYReadBookIndexController:UITableViewDataSource,UITableViewDelegate{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: self.Identifier, for: indexPath)as!XYBookListCell
        cell.model = dataArr[indexPath.row]

        return cell
    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }


    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if dataArr.count == 0 {
            return
        }
        // 转交出去,给外部实现
        didSelectRowAt?(indexPath.row)
    }


    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerView = XYReadBookIndexHeaderView.init(frame: CGRect.init(x: 0, y: 0, width: ScreenWidth * 0.8, height: 44), chapterCount: dataArr.count + 1)
        headerView.delegate = self
        return headerView
    }
}


extension XYReadBookIndexController:XYReadBookIndexHeaderViewDelegate {
    func booklistRefresh(bookListView: XYReadBookIndexHeaderView) {
        Refresh()
    }

    func bookChapterlistSort(bookListView: XYReadBookIndexHeaderView) {
        XYLog(messsage: "排序")
    }
}

